package service.impl;

import dao.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pojo.SysUser;
import service.SysUserService;

import java.util.List;
import java.util.Objects;

@Scope("prototype")
@Transactional
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public List<SysUser> getList(SysUser ysUser) {
        return sysUserMapper.selectSysUserList(ysUser);
    }

    @Override
    public boolean add(SysUser sysUser) {
        boolean result=false;
        try {
            if (sysUserMapper.add(sysUser)>0) {
                result=true;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(int id) {
        boolean result=false;
        try {
            if (sysUserMapper.delete(id)>0) {
                result=true;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 根据id修改用户
     *
     * @param sysUser
     * @return
     */
    @Override
    public boolean update(SysUser sysUser) {
        boolean result=false;
        try {
            if (sysUserMapper.update(sysUser)>0) {
                result=true;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 根据角色id删除用户
     *
     * @param roleId
     * @return
     */
    @Override
    public boolean delUserByRoleId(int roleId) {
        boolean result=false;
        try {
            if(sysUserMapper.delUserByRoleId(roleId)>0){
                result=true;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据账号密码查询用户，即实现登录的功能
     *
     * @param account
     * @param pwd
     * @return
     */
    @Override
    public SysUser login(String account, String pwd) {
        SysUser sysUser=null;
        try {
            sysUser=sysUserMapper.selectByAccount(account);
            if (sysUser!=null) {
                if (!Objects.equals(sysUser.getPassword(), pwd)) {
                    sysUser=null;
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return sysUser;
    }

    /**
     * 根据账号密码查询用户，即实现登录的功能
     *
     * @param account
     * @param pwd
     * @return
     */
    @Override
    public SysUser queryByAccountAndPwd(String account, String pwd) {
        return sysUserMapper.queryByAccountAndPwd(account,pwd);
    }

    /**
     * 根据角色名称、角色id查询总数据量
     *
     * @param realName
     * @param roleId
     * @return
     */
    @Override
    public int getUserCount(String realName, int roleId) {
        return sysUserMapper.getUserCount(realName,roleId);
    }

    /**
     * 分页查询
     *
     * @param realName
     * @param roleId
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public List<SysUser> getUserList(String realName, int roleId, int pageIndex, int pageSize) {
        return sysUserMapper.getUserList(realName,roleId,pageIndex,pageSize);
    }

    /**
     * 根据账号查询用户
     *
     * @param account
     * @return
     */
    @Override
    public List<SysUser> queryByAccount(String account) {
        return sysUserMapper.queryByAccount(account);
    }

    @Override
    public SysUser getUserById(String id) {
        return sysUserMapper.getUserById(id);
    }

    @Override
    public int userExist(String account) {
        return sysUserMapper.userExist(account);
    }




    public SysUserMapper getSysUserMapper() {
        return sysUserMapper;
    }

    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }
}
